/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.ChargeCheckItemBean;
import com.naizhuai.ui.entity.VirtualChosenBean;
import com.naizhuai.util.DeviceUtils;
import com.naizhuai.util.ImageUtils;

import java.util.List;

public class ChosenChargeJoinAdapterHolder extends RecyclerView.Adapter {

    public final static int TYPE_CHOSEN_DEPARTMENT = 1;
    public final static int TYPE_ADD_PERSON = 2;
    public final static int TYPE_ADD_PERSON_MANAGE = 3;
    public final static int TYPE_CHOSEN_PERSON = 4;
    public final static int TYPE_CHOSEN_MANAGE = 5;
    public final static int TYPE_THANKS_PERSON = 6;
    public final static int TYPE_CHOSEN_TEAM_MAIN_MANAGER = 7;

    public static final String ENTER_TYPE_KEY = "enter_type_key";
    public static final String CHOSEN_TYPE_KEY = "chosen_type_key";


    public final static int TYPE_CLICK_DETAILS = 3;
    public final static int TYPE_CLICK_CHOSEN = 4;

    private OnItemClickListener mItemClickListener;
    private BaseActivity mContext;
    private List<ChargeCheckItemBean<VirtualChosenBean>> virBean;
    private int chosenType;


    public ChosenChargeJoinAdapterHolder(BaseActivity context, List<ChargeCheckItemBean<VirtualChosenBean>> virBean, int chosenType) {
        this.mContext = context;
        this.virBean = virBean;
        this.chosenType = chosenType;
    }

    @Override
    public int getItemViewType(int position) {
        return chosenType;
    }

    @Override
    public int getItemCount() {
        return virBean.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType != TYPE_CHOSEN_DEPARTMENT) {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_virtual_chosen_person_item_content, parent, false);
            return new PersonItemHolder(sView);
        } else {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_virtual_chosen_department_item_content, parent, false);
            return new DepartmentItemHolder(sView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PersonItemHolder) {
            PersonItemHolder personHolder = (PersonItemHolder) holder;
            personHolder.checkedImg.setSelected(virBean.get(position).isCheck());
            VirtualChosenBean bean = virBean.get(position).getPersonUser();
            ImageUtils.glideThumbViewByUrl(mContext, personHolder.userThumb, bean.getUserThumb());
            personHolder.userName.setText(bean.getName());
            personHolder.userLevel.setText(bean.getUserLevel());
            personHolder.actionNumber.setText(bean.getShowLabels().get(0).getValue());
            personHolder.pressNumber.setText(bean.getShowLabels().get(1).getValue());
        } else if (holder instanceof DepartmentItemHolder) {
            DepartmentItemHolder departHolder = (DepartmentItemHolder) holder;
            departHolder.checkedImg.setSelected(virBean.get(position).isCheck());
            VirtualChosenBean bean = virBean.get(position).getPersonUser();
            departHolder.departName.setText(bean.getName());
            departHolder.departNumber.setText(bean.getShowLabels().get(0).getValue());
        }
    }


    public class PersonItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView checkedImg;
        CircleImageView userThumb;
        TextView userName, userLevel, actionNumber, pressNumber;

        public PersonItemHolder(View view) {
            super(view);
            checkedImg = (ImageView) view.findViewById(R.id.chosen_select);
            userThumb = (CircleImageView) view.findViewById(R.id.user_thumb);
            userName = (TextView) view.findViewById(R.id.user_name);
            userLevel = (TextView) view.findViewById(R.id.user_level);
            actionNumber = (TextView) view.findViewById(R.id.action_point_number);
            pressNumber = (TextView) view.findViewById(R.id.press_number);
            checkedImg.setOnClickListener(this);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.chosen_select:
                    if (mItemClickListener != null)
                        mItemClickListener.onItemClick(v, getAdapterPosition(), TYPE_CLICK_CHOSEN);
                    break;
                default:
                    if (mItemClickListener != null)
                        mItemClickListener.onItemClick(v, getAdapterPosition(), TYPE_CLICK_DETAILS);
                    break;
            }
        }
    }

    public class DepartmentItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView checkedImg;
        TextView departName, departNumber;

        public DepartmentItemHolder(View view) {
            super(view);
            checkedImg = (ImageView) view.findViewById(R.id.chosen_select);
            departName = (TextView) view.findViewById(R.id.department_name);
            departNumber = (TextView) view.findViewById(R.id.department_number);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null)
                mItemClickListener.onItemClick(v, getAdapterPosition(), TYPE_CLICK_CHOSEN);
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position, int clickType);
    }

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

}
